<!DOCTYPE html>
<html lang="en">
    <head>
    <title>Rust - Exploring Lifetime Problems - 1 - Tinusgraglin</title>
    
    
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1"><meta name="description" content="A Slice Rust - Exploring Lifetime Problems - 1 How should we understand this?"/><meta name="keywords" content="rust, rust-language, rust-lifetime" />
    <meta property="og:title" content="A Slice of Tgl -&nbsp;Rust - Exploring Lifetime Problems - 1" />
    <meta property="og:type" content="website"/><meta property="og:url" content="&#x2F;blog&#x2F;rust-explore-lifetime-problems-1&#x2F;"/><meta property="og:description" content="How should we understand this?"/>
    <link rel="preload" href="/blog/assets/fonts/FiraCode-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" href="/blog/assets/fonts/FiraCode-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

    <link rel="stylesheet" href="/blog/style.css?h=97e262212a0704163528">
    <link rel="stylesheet" href=" /blog/color/orange.css?h=a5a5d7faf9d4bc3e0b18">
    
</head>
    <body>
        <div class="container center">
<header class="header">
    <div class="header__inner">
        <div class="header__logo">
            <a href="&#x2F;blog">
    <div class="logo">
        LIN
    </div>
</a>
        </div>
        <div class="menu-trigger">menu</div>
    </div>
    
    <nav class="menu">
        <ul class="menu__inner menu__inner--desktop">
            
            
                    <li>
                        <a href="
    
        /blog/about
    
">about</a>
                    </li>
                
            </ul>

        <ul class="menu__inner menu__inner--mobile">
            
        <li>
            <a href="
    
        /blog/about
    
">about</a>
        </li>
        </ul>
    </nav>

    </header>
<div class="content"><article class="post">
        <header>
            <h1 class="post-title">
                <a href="&#x2F;blog&#x2F;rust-explore-lifetime-problems-1&#x2F;">Rust - Exploring Lifetime Problems - 1</a>
            </h1>
            
    <div class="post-meta">
        <span class="post-date">2023.01.22
                [Updated: 2023.12.28]
                </span>

        <span class="post-author"></span>

        

    
    :: {<a href="/blog/categories/rust/">rust</a>} 

            
    ::
    #<a href="/blog/tags/rust-language/">rust-language</a>
        
    #<a href="/blog/tags/rust-lifetime/">rust-lifetime</a>
        
    
            
        
    </div>

            
    
</header><p>The following code snippet is adapted from an example from <a href="http://zderadicka.eu/higher-rank/">this blog post</a>:</p>
<pre data-lang="rust" style="background-color:#2b303b;color:#c0c5ce;" class="language-rust "><code class="language-rust" data-lang="rust"><span style="color:#b48ead;">trait </span><span>Checksum&lt;R: Read&gt; {
</span><span>    </span><span style="color:#b48ead;">fn </span><span style="color:#8fa1b3;">calc</span><span>(&amp;</span><span style="color:#bf616a;">self</span><span>, </span><span style="color:#bf616a;">r</span><span>: R) -&gt; Vec&lt;</span><span style="color:#b48ead;">u8</span><span>&gt;;
</span><span>}
</span><span>
</span><span style="color:#b48ead;">struct </span><span>XorChecksum;
</span><span style="color:#b48ead;">impl</span><span>&lt;</span><span style="color:#b48ead;">&#39;a</span><span>&gt; Checksum&lt;&amp;</span><span style="color:#b48ead;">&#39;a </span><span>[</span><span style="color:#b48ead;">u8</span><span>]&gt; </span><span style="color:#b48ead;">for </span><span>XorChecksum {
</span><span>    </span><span style="color:#b48ead;">fn </span><span style="color:#8fa1b3;">calc</span><span>(&amp;</span><span style="color:#bf616a;">self</span><span>, </span><span style="color:#bf616a;">r</span><span>: &amp;</span><span style="color:#b48ead;">&#39;a</span><span> [</span><span style="color:#b48ead;">u8</span><span>]) -&gt; Vec&lt;</span><span style="color:#b48ead;">u8</span><span>&gt; { todo!() }
</span><span>}
</span><span>
</span><span style="color:#b48ead;">fn </span><span style="color:#8fa1b3;">main</span><span>() {
</span><span>    </span><span style="color:#b48ead;">let mut</span><span> buf = [</span><span style="color:#d08770;">0</span><span style="color:#b48ead;">u8</span><span>; </span><span style="color:#d08770;">8</span><span>];
</span><span>    </span><span style="color:#b48ead;">let</span><span> xor_checker = XorChecksum;
</span><span>    </span><span style="color:#b48ead;">let</span><span> checker: &amp;dyn Checksum&lt;&amp;[</span><span style="color:#b48ead;">u8</span><span>]&gt; = &amp;xor_checker;
</span><span>    </span><span style="color:#65737e;">// Type: &amp;&#39;l1 dyn (Checksum&lt;&amp;&#39;l2 [u8]&gt; + &#39;l3)
</span><span>    </span><span style="color:#65737e;">// 1.&#39;l3: &#39;l1
</span><span>    </span><span style="color:#65737e;">// 2.&#39;l2: &#39;l3
</span><span>    </span><span style="color:#65737e;">// =&gt; &#39;l2: &#39;l1
</span><span>    </span><span style="color:#65737e;">//
</span><span>    </span><span style="color:#65737e;">// -------------------------------------------
</span><span>    </span><span style="color:#65737e;">// Type: &amp;&#39;l1 Xor
</span><span>    </span><span style="color:#65737e;">//
</span><span>    </span><span style="color:#65737e;">// -------------------------------------------
</span><span>    </span><span style="color:#65737e;">// Type: &amp;&#39;l1 Xor&lt;&#39;l2&gt;
</span><span>    </span><span style="color:#65737e;">// 1.&#39;Xor&lt;&#39;l2&gt;: &#39;l1
</span><span>    </span><span style="color:#65737e;">// 2.&#39;l2: &#39;Xor&lt;&#39;l2&gt;
</span><span>    </span><span style="color:#65737e;">// =&gt; &#39;l2: &#39;l1
</span><span>    checker.</span><span style="color:#96b5b4;">calc</span><span>(&amp;buf[</span><span style="color:#d08770;">1</span><span>..]); </span><span style="color:#65737e;">// immutable borrow occurs here
</span><span>    &amp;</span><span style="color:#b48ead;">mut</span><span> buf;                </span><span style="color:#65737e;">// can not mutably borrow!
</span><span>    checker;                 </span><span style="color:#65737e;">// immutable borrow used here
</span><span>}
</span></code></pre>
<p>My analysis is as follows:</p>
<p><code>checker</code> is of type <code>&amp;'l1 (dyn Checksum&lt;&amp;'l2 [u8]&gt; + 'l3)</code>, since for any <code>&amp;'l T</code>,
<code>T: 'l</code>, we have <code>'l3: 'l1</code>. And for any <code>T</code> that implements <code>Checksum&lt;&amp;'l2 [u8]&gt;</code>,
<code>'l2: T</code>, so we have <code>'l2: 'l3</code>. Thus <code>'l2: 'l1</code>, i.e. the <code>&amp;[u8]</code> this <code>checker</code> internally
borrows lives as long as <code>checker</code>.</p>
<p>Now looking back at the <code>dyn Checksum&lt;&amp;'l2 [u8]&gt;</code> part, we seem to demand a type that implements
<code>Checksum&lt;&amp;'l2 [u8]&gt;</code> for some specific fixed <code>'l2</code>, the checker becomes a checker for some
specific <code>&amp;[u8]</code>, but what we want is actually a checker for any <code>&amp;[u8]</code>:</p>
<pre data-lang="rust" style="background-color:#2b303b;color:#c0c5ce;" class="language-rust "><code class="language-rust" data-lang="rust"><span style="color:#b48ead;">let</span><span> checker: &amp;dyn </span><span style="color:#b48ead;">for</span><span>&lt;</span><span style="color:#b48ead;">&#39;a</span><span>&gt; Checksum&lt;&amp;</span><span style="color:#b48ead;">&#39;a </span><span>[</span><span style="color:#b48ead;">u8</span><span>]&gt; = &amp;xor_checker;
</span></code></pre>
<p>Indeed, the <code>impl Checksum</code> block for <code>XorChecksum</code> is actually defining <code>XorChecksum</code>'s <code>Checksum</code>
implementation for any <code>&amp;'a [u8]</code>:</p>
<pre data-lang="rust" style="background-color:#2b303b;color:#c0c5ce;" class="language-rust "><code class="language-rust" data-lang="rust"><span style="color:#b48ead;">impl</span><span>&lt;</span><span style="color:#b48ead;">&#39;a</span><span>&gt; Checksum&lt;&amp;</span><span style="color:#b48ead;">&#39;a </span><span>[</span><span style="color:#b48ead;">u8</span><span>]&gt; </span><span style="color:#b48ead;">for </span><span>XorChecksum { .. }
</span></code></pre>
<p>So it is a <code>dyn for&lt;'a&gt; Checksum&lt;&amp;'a [u8]&gt;</code>. And since a checker for any <code>&amp;[u8]</code> would work
for a specific <code>&amp;[u8]</code>, it is also a <code>dyn Checksum&lt;&amp;'l [u8]&gt;</code>.</p>


        
    

        
        
    </article></div>
    <div class="pagination">
        <div class="pagination__buttons">
            </div>
    </div>
<footer class="footer">
                    <div class="footer__inner"><div class="copyright">
            <span>© 2024 Tinusgraglin <span/>
            <span>:: Powered by <a href="https://www.getzola.org/">Zola</a>(<a href="https://www.rust-lang.org/">Rust</a>) and <a href="https://bun.sh">Bun</a>(<a href="https://ziglang.org">Zig</a>)</span>
        </div>
    <script type="text/javascript" src="/blog/assets/js/main.js"></script>
</div>
                    

                </footer></div>
    </body>
</html>
